#!/anaconda3/envs/FEALPy/bin python3.8
# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: run_youbangzilu.py
# @Author: ---
# @Institution: --- University, ---, China
# @E-mail: ---@---.com, ---@---.edu.cn
# @Site: 
# @Time: 11月 18, 2024
# ---

import os
import datetime
import pytest
import pandas as pd
from tqdm import tqdm
from conftest import TestResultCollector, TEST_CASES_PATH  # 引入全局变量

# 确保 excels 目录存在
excel_dir = "excels"
if not os.path.exists(excel_dir):
    os.makedirs(excel_dir)

# 生成Excel文件名
now = datetime.datetime.now()
excel_name = f"友邦自录_{now.strftime('%Y%m%d_%H%M')}.xlsx"
excel_path = os.path.join(excel_dir, excel_name)


def write_results_to_excel(results):
    df = pd.DataFrame(results)
    df.to_excel(excel_path, index=False)


def main():
    # 读取测试用例文件
    df = pd.read_excel(TEST_CASES_PATH)
    total_tests = len(df)

    # 初始化进度条
    progress_bar = tqdm(total=total_tests, desc="测试进度")

    # 定义一个空列表来保存测试结果
    test_results = []

    # 初始化TestResultCollector插件
    collector = TestResultCollector(test_results, total_tests, progress_bar)

    # 运行测试并将结果收集到 test_results 列表中

    pytest.main(["-v", "testcase/test_youbangZilu.py"], plugins=[collector])


    # 将测试结果写入 Excel 文件
    write_results_to_excel(test_results)

    print(f"测试结果已写入: {excel_path}")
    progress_bar.close()


if __name__ == "__main__":
    main()